package com.ztsoft.user.controller.dynamic;

import com.alibaba.excel.EasyExcelFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ztsoft.user.common.base.BaseController;
import com.ztsoft.user.common.exception.CustomException;
import com.ztsoft.user.pojo.entity.dynamic.DynamicTable;
import com.ztsoft.user.pojo.enums.ResultCode;
import com.ztsoft.user.pojo.vo.PageData;
import com.ztsoft.user.pojo.vo.PageInfo;
import com.ztsoft.user.pojo.vo.ResultData;
import com.ztsoft.user.pojo.vo.dynamic.AdvancedSearchVo;
import com.ztsoft.user.pojo.vo.dynamic.BatchCoseVo;
import com.ztsoft.user.pojo.vo.dynamic.SearchItemVo;
import com.ztsoft.user.service.dynamic.DynamicTableService;
import com.ztsoft.user.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/dynamicTable")
@RestController
@CrossOrigin("*")
@Api(tags = "动态表格模块")
public class DynamicTableController extends BaseController<DynamicTable, String, DynamicTableService> {

    @Autowired
    private DynamicTableService dynamicTableService;

    @PostMapping("/exportFile")
    @ApiOperation(value = "导出测试")
    public void exportFile(HttpServletResponse response, HttpServletRequest request) {
        try {
            List<BatchCoseVo> list = new ArrayList<>();
            BatchCoseVo batchCoseVo = new BatchCoseVo();
            list.add(batchCoseVo);

            File file = new File("C:/dataFile/exportWord/batchCoseExport.xlsx");
            InputStream inputStream = new FileInputStream(file);

            EasyExcelFactory.write(response.getOutputStream(), BatchCoseVo.class).needHead(false).withTemplate(inputStream).sheet(0).doWrite(list);

        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(ResultCode.FILE_DOWNLOAD_FAIL.getMsg(), ResultCode.FILE_DOWNLOAD_FAIL.getCode());
        }
    }

    @Override
    public Result saveOrUpdate(@RequestBody DynamicTable dynamicTable) {
        QueryWrapper<DynamicTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("table_code", dynamicTable.getTableCode());
        List<DynamicTable> list = baseService.list(queryWrapper);

        if (!list.isEmpty()) {
            return Result.returnMsg("该table_code码已经存在！", ResultCode.FAILED.getCode());
        }

        baseService.saveDynamicTable(dynamicTable);
        return Result.success();
    }

    @GetMapping("/queryTableByTableCode")
    @ApiOperation(value = "根据表格编码返回表格相关信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "tableCode", value = "表格编码", required = true, paramType = "form")})
    public Result<Object> queryTableByTableCode(String tableCode) {
        ResultData map = baseService.queryTableByTableCode(tableCode);
        return Result.returnDataMsg(ResultCode.SUCCESS.getMsg(), ResultCode.SUCCESS.getCode(), map);
    }


    @GetMapping("/querySearchByTableCode")
    public Result querySearchByTableCode(String tableCode) {
        List<AdvancedSearchVo> list = baseService.querySearchByTableCode(tableCode);
        return Result.returnDataMsg(ResultCode.SUCCESS.getMsg(), ResultCode.SUCCESS.getCode(), list);
    }

    //获取所有表格编码
    @GetMapping("/getAllTableCode")
    @ApiOperation(value = "获取所有表格编码")
    public Result getAllTableCode() {
        List<String> list = baseService.getAllTableCode();
        return Result.returnDataMsg(ResultCode.SUCCESS.getMsg(), ResultCode.SUCCESS.getCode(), list);
    }

    @PutMapping("/updateById")
    public Result updateById(@RequestBody DynamicTable t) {
        try {
            Date date = new Date();
            t.setModifyTime(date);
            baseService.updateById(t);
            return Result.success();
        } catch (Exception e) {
            throw new CustomException(ResultCode.ERROR.getMsg(), ResultCode.ERROR.getCode());
        }
    }


    //////////////////////////////////////////////////////////////////改造///////////////////////////////////////////////////////

    /**
     * 带条件分页查询
     *
     * @param dynamicTable 条件实体
     * @param currentPage  当前页
     * @param pageSize     当前页面大小
     * @return 结果值
     */
    @GetMapping("/queryDataByPage")
    public Result<Object> queryDataByPage(DynamicTable dynamicTable, Integer currentPage, Integer pageSize) {
        return dynamicTableService.queryDataByPage(dynamicTable, currentPage, pageSize);
    }

    @GetMapping("/queryDataByTableCode/{tableCode}/{pageSize}/{currentPage}")
    @ApiOperation(value = "根据表格编码返回数据")
    public Result<Object> queryDataByTableCode(@PathVariable("tableCode") String tableCode, @PathVariable("pageSize") Long pageSize, @PathVariable("currentPage") Long currentPage, @RequestParam Map<String, Object> map) {
        PageInfo pageInfo = new PageInfo(pageSize, currentPage, 0);
        SearchItemVo searchItemVo = new SearchItemVo(map);
        PageData pageData = baseService.queryDataByTableCode(tableCode, pageInfo, searchItemVo);
        return Result.returnDataMsg(ResultCode.SUCCESS.getMsg(), ResultCode.SUCCESS.getCode(), pageData);
    }

}